package com.whz.enummap.plant;

import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author: sinosoft
 * @Description: 使用数据结构 EnumMap 完成枚举映射
 * 没有经过不安全的转换
 * 不必手工标注这些索引的输出
 * @Date: Create in 10:16 2021/5/17
 * @Modified By:
 */
public class PlantMainTest_EnumMap {
    public static void main(String[] args) {
        Plant[] garden = new Plant[] {
                new Plant("白菊花", Plant.LifeCycle.ANNUAL),
                new Plant("黄菊花", Plant.LifeCycle.ANNUAL),
                new Plant("橄榄菊", Plant.LifeCycle.BIENNIAL),
//                new Plant("银冠花", Plant.LifeCycle.PERENNIAL)
        };
        /*
         * 使用EnumMap完成枚举到值的映射
         * */
        Map<Plant.LifeCycle, Set<Plant>> plantsByLifeCycle = new EnumMap<>(Plant.LifeCycle.class);
        for (Plant.LifeCycle lc : Plant.LifeCycle.values()) {
            plantsByLifeCycle.put(lc, new HashSet<>());
        }

        for (Plant p : garden) {
            plantsByLifeCycle.get(p.lifeCycle).add(p);
        }
        System.out.println(plantsByLifeCycle);
    }
}
